// pages/mainOperation/cityList/cityList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cityList: [], //医院列表
    cityLetter: [], //医院首字母
    cityGroup: [], //医院列表分组
    currentLetterIndex: 0, //当前被选中的首字母
    toId: 'A', //所要前往的模块Id
    winHeight: '', //滚动区域的高度
    inputValue: '', //输入框中的值
    itemHeight: '',
    searchLetterText: null,
    isShowSearchLetterText: false,
    disabledSearchLetter: false,
    noDataText: '未查询到您输入的内容', //暂无数据的消息提示文本
    noData: false, //是否显示暂无数据
    noDataHeight: '330', //暂无数据的高度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    self.getHospitals();
  },
  /**
   * 获取体检医院
   */
  getHospitals() {
    let self = this;
    let url = app.apiBaseUrl + "/Common/GetAllOpenCity";

    app.httpRequest({
      method: "GET",
      url: url,
      showLoading: true,
      data: {
        cityId: app.locationInfo.cityId
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          let cityList = rdata.data;
          let cityLetter = self.getLetter(cityList);
          let cityGroup = self.cityFilter(cityLetter, cityList);
          console.log(cityLetter)
          console.log(cityGroup)
          self.setData({
            cityList: cityList,
            cityLetter: cityLetter,
            cityGroup: cityGroup
          });
          self.initData();
        } else {
          wx.showToast({
            title: rdata.message
          });
        }
      }
    });
  },
  /**
   * 获取首字母
   */
  getLetter(cityList) {
    var tmpArr = [];
    for (let i = 0; i < cityList.length; i++) {
      if (tmpArr.indexOf(cityList[i].Initial) <= 0) {
        tmpArr.push(cityList[i].Initial);
      }
    }
    return tmpArr;
  },
  /**
   * 对医院信息进行分组
   */
  cityFilter(cityLetter, cityList) {
    let tempArr = [];
    cityLetter.map(
      Initial => {
        let tempObj = {};
        tempObj.Initial = Initial;
        tempObj.cityInfo = cityList.filter(
          item => item.Initial == Initial
        );
        if (tempObj.cityInfo.length > 0) {
          tempArr.push(tempObj);
        }
      }
    );
    return tempArr;
  },
  /**
   * 初始化数据
   */
  initData() {
    let self = this;
    //这里需要对可使用窗口高度进行重新获取
    let windowHeight;
    wx.getSystemInfo({
      success(res) {
        windowHeight = res.windowHeight;
      },
      fail(err) {
        console.log('获取系统信息失败！');
      }
    })
    let resHeight = windowHeight - 56;
    let itemHeight = 24;
    let letterLength = self.data.cityLetter.length;
    if (letterLength >= 20) {
      itemHeight = parseInt(resHeight / letterLength);
    } else {
      itemHeight = 24
    }
    self.setData({
      winHeight: resHeight,
      itemHeight: itemHeight
    })
  },
  /**
   *  首字母选择
   */
  choiceLetterTap: function(e) {
    let self = this;
    let index = e.currentTarget.dataset.index;
    let cityLetter = self.data.cityLetter;
    let toId = cityLetter[index];
    if (!self.data.disabledSearchLetter) {
      self.setData({
        searchLetterText: self.data.cityLetter[index],
        isShowSearchLetterText: true
      })
      setTimeout(function() {
        self.setData({
          isShowSearchLetterText: false
        })
      }, 1500);
      self.setData({
        currentLetterIndex: index,
        toId: toId
      })
    }
  },
  /**
   *  用户输入input事件
   */
  searchInputTap(e) {
    let self = this;
    let value = e.detail.value.trim();
    self.setData({
      inputValue: value
    })
    let inputValue = self.data.inputValue.toLowerCase();
    let cityList = self.data.cityList;
    let cityLetter = self.data.cityLetter;
    let cityGroup = self.data.cityGroup;
    if (self.data.inputValue) {
      //input有值
      self.setData({
        disabledSearchLetter: true
      })
      let tmpHosistList = cityList.filter(
        item => (item.CitySpellName.indexOf(inputValue) >= 0)
      );
      let tmpHostList1 = cityList.filter(
        item => (item.CityName.indexOf(inputValue) >= 0)
      );
      tmpHosistList = tmpHosistList.concat(tmpHostList1);

      if (tmpHosistList.length > 0) {
        let cityGroup = self.cityFilter(self.data.cityLetter, tmpHosistList);
        console.log(cityGroup)
        self.setData({
          cityGroup: cityGroup,
          noData: false
        })
      } else {
        self.setData({
          cityGroup: [],
          noData: true
        })
      }
    }
  },
  /**
   *  表单列表点击
   */
  listItemTap: function(e) {
    let self = this;
    let cityid = e.currentTarget.dataset.cityid;
    let cityname = e.currentTarget.dataset.cityname;
    app.locationInfo.cityId = cityid;
    app.locationInfo.cityName = cityname;

    //获取上一页
    let pages = getCurrentPages();
    let prePage = pages[pages.length - 2];
    prePage.setData({
	  locationCityId:cityid,
	  locationCity:cityname
    });

    prePage.getBizTypes();

    wx.navigateBack({
      delta: 1 // 返回上一级页面。
    });
  },
  /**
   *  清除输入框表单内容
   */
  delInputValue: function() {
    let self = this;
    self.setData({
      inputValue: ''
    })
    let cityGroup = self.cityFilter(self.data.cityLetter, self.data.cityList);
    self.setData({
      disabledSearchLetter: false,
      cityGroup: cityGroup,
      noData: false
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})